<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary" class="add-btn" size="mini" @click="showHandler(0,1)">添加权限</el-button>
      <el-table align="center" :data="list" row-key="id" default-expand-all>
        <el-table-column align="center" label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" size="mini" @click="showHandler(row.id,2)">添加</el-button>
            <el-button type="text" size="mini" @click="showHandler(row.id)">编辑</el-button>
            <el-popconfirm title="这是一条权限点内容确定删除吗？" style="margin-left:10px;" @onConfirm="delPermission(row.id)">
              <el-button slot="reference" type="text" size="mini">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹出层 -->
    <el-dialog :title="showText" :visible.sync="showDialog" width="500px" @close="btnCancel">
      <el-form ref="form" label-width="120px" :model="formData" :rules="formRules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" size="mini" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" size="mini" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :rows="3" size="mini" />
        </el-form-item>
        <el-form-item label="开启" prop="enabled">
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" size="mini" />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="7">
            <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
            <el-button size="mini" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, getPermissionDetail, updatePermission, addPermission, delPermission } from '@/api/permission'
import { transformListToTreeData } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: '', // 权限点类型,1为页面路由权限点添加，2为按钮权限点添加
        pid: '', // 父级权限点id
        enVisible: '0' // 权限点开启状态，默认关闭
      },
      formRules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      // 根据当前表格数据中是否包含id属性来动态的标题显示文本
      return this.formData.id ? '编辑权限点' : '新增权限点'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transformListToTreeData(await getPermissionList(), 0)
    },
    async showHandler(id, type) {
      this.showDialog = true
      // 添加权限点
      if (type === 1 || type === 2) {
        this.formData.pid = id
        this.formData.type = type
      } else {
        // 编辑权限点
        this.formData = await getPermissionDetail(id)
      }
    },
    btnCancel() {
      // 重置表单
      this.formData = {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: '', // 权限点类型,1为页面路由权限点添加，2为按钮权限点添加
        pid: '', // 父级权限点id
        enVisible: '0' // 权限点开启状态，默认关闭
      }
      this.showDialog = false
      this.$refs.form.resetFields()
    },
    btnOk() {
      this.$refs.form.validate(async isOk => {
        if (isOk) {
          // 更新权限点
          let msg
          // 如果有id证明是编辑状态
          if (this.formData.id) {
            msg = '更新权限点成功'
            await updatePermission(this.formData)
          } else {
            // 否则证明是添加权限点状态
            msg = '添加权限点成功'
            await addPermission(this.formData)
          }
          // 发送成功消息
          this.$message.success(msg)
          // 重新获取权限点数据列表
          this.getPermissionList()
          // 关闭弹窗
          this.showDialog = false
        }
      })
    },
    async delPermission(id) {
      // 调用删除权限点的接口
      await delPermission(id)
      // 重新获取权限点数据列表
      this.getPermissionList()
      // 显示删除成功提示框
      this.$message.success('删除权限点成功')
    }
  }
}
</script>
<style scoped>
.add-btn {
  margin:10px;
}
</style>
